package hackthon.service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import hackthon.core.Constant;
import hackthon.core.MyRedis;
import hackthon.dao.LoginDao;
import hackthon.pojo.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class LoginService {


    @Autowired
    private LoginDao loginDao;

    @Autowired
    private MyRedis myRedis;

    /**this redis login user auth can be modified to be more perfect, such as add the version number to check
     * if the current user version number is the latest, if not we should check the database, and update the
     * redis cache so that the data can be correct and keep the data consistency.(due to some reasons, this function
     * we ignore it now, if possible, i want to realize it next project or next version)
     **/
    public boolean CookieLogin(String token){

        token = Constant.decodeBASE64(token);

        //if the redis cache doesn't contain the current user, we should check the database and restore it into redis
        if(!myRedis.existsKey(token)){
            String key[] = token.split("_");
            String username = key[0];
            String password = key[1];
            int loginStatus = this.isLogin(username, password, Constant.COOKIE_LOGIN);
            if(loginStatus == Constant.LOGIN_STATUS_LOGIN_SUCCESS){
                return true;
            }
            return false;
        }
        String loginInfo = myRedis.getStr(token);
        if(loginInfo == null){
            return  false;
        }else {
            return true;
        }

    }

    //restore userinfo
    public void storeLoginInfo(String username, String password, int loginType) throws Exception {
        String key = "";
        if(loginType == Constant.USERNAME_PASS_LOGIN){
            key = username + "_" + password;
        }else if(loginType == Constant.COOKIE_LOGIN){
            key = username + "_" + password;
        }
        UserInfo employee = loginDao.getUserByUsername(username);
        if(employee == null){
            throw new Exception("the username has been force changed, please care!!!");
        }
        myRedis.setObject(key, employee);
    }

    //check the user login or not
    public int isLogin(String username, String password, int loginType){
        //username null
        if(username == null || username.trim().equals("")){
            return Constant.LOGIN_STATUS_ERRORUSERNAME;
        }
        //password null
        if(password == null || password.trim().equals("")){
            return Constant.LOGIN_STATUS_ERRORPASSWORD;
        }
        UserInfo emp = loginDao.getUserByUsername(username.trim());

        //the user is not exists
        if(emp == null){
            return Constant.LOGIN_STATUS_ERRORUSERNAME;
        }

        if(loginType == Constant.USERNAME_PASS_LOGIN){
           password = password.trim();
        }else if(loginType == Constant.COOKIE_LOGIN){
           password = password.trim();
        }
        // the user exists, but the password is not matched
        if(!password.equals(emp.getPassword())){
            return Constant.LOGIN_STATUS_ERRORPASSWORD;
        }
        try {
            this.storeLoginInfo(username,password,loginType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Constant.LOGIN_STATUS_LOGIN_SUCCESS;
    }

    public UserInfo getUserByUserName(String username){
        return loginDao.getUserByUsername(username);
    }
}
